package com.kedou.sdk.utils;

import android.content.Context;
import android.text.TextUtils;
import android.util.Xml;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.util.HashMap;

/**
 * Created by Administrator on 2017/6/2 0002.
 */

public final class XmlUtils {

    private static final String TAG = XmlUtils.class.getSimpleName();

    private static final String CONFIG = "channel_config.xml";
    private static final String SDK_CONFIG = "channel_sdk_config.xml";
    private static final String INNER_SDK_CONFIG = "inner_sdk_config.xml";
    private static final String CHANNEL_GAME_CONFIG = "channel_game.xml";
    private static final String DEFAULT_SECTET = "default";

    private static final HashMap<String, String> SDK_MAPS = new HashMap<String, String>();

    private static final HashMap<String, String> SECRET_MAPS = new HashMap<String, String>();

    private static boolean isInitFinished = false;

    static {
        SECRET_MAPS.put(DEFAULT_SECTET, "YLTWbZoZ4DCQ1zrcN3rqi77ajKLwhuHv");
    }

    public static void parseConfigValue(Context context) {
        parseChannelValue(context);
        parseChannelSdkValue(context);
        parseInnerSdkValue(context);
        parseChannelGameValue(context);
        isInitFinished = true;
    }



    private static void parseChannelValue(Context context) {
        InputStream inputStream = null;
        try {
            inputStream = context.getAssets().open(CONFIG);
            XmlPullParser xmlPullParser = Xml.newPullParser();
            xmlPullParser.setInput(inputStream, "UTF-8");
            int eventType = xmlPullParser.getEventType();
            while (XmlPullParser.END_DOCUMENT != eventType) {
                if (XmlPullParser.START_TAG == eventType) {
                    String element = xmlPullParser.getName();
                    if ("channel_id".equals(element) || "chanel_id".equals(element)) {
                        SDK_MAPS.put("channelId", xmlPullParser.nextText());
                    }

                    if ("channel_key".equals(element)) {
                        SDK_MAPS.put("channel_key", xmlPullParser.nextText());
                    }
                }
                eventType = xmlPullParser.next();
            }
        } catch (XmlPullParserException e) {
            WL.e(TAG, "parseChannelValue ----> XmlPullParserException");
        } catch (IOException e) {
            WL.e(TAG, "parseChannelValue ----> IOException");
        } finally {
            IoUtils.closeSilently(inputStream);
        }
    }

//    private static void parseChannelValue(Context context) {
//        byte[] key = "abcdefghigklmn".getBytes();
//        int keyLength = key.length;
//        InputStream inputStream = null;
////        InputStreamReader isr = null;
//        try {
//            inputStream = context.getAssets().open(SDK_CONFIG);
////            isr = new InputStreamReader(inputStream,"UTF-8");
////            char[] ch=new char[2014];
////            int len=isr.read(ch);
//            byte[] ch=new byte[2014];
//            int len = inputStream.read(ch);
//            for (int n = 0; n < ch.length; n++) {
//                ch[n] ^= key[n % keyLength];
//            }
//
//            WL.e(TAG, new String(ch,0,len));
//            XmlPullParser xmlPullParser = Xml.newPullParser();
//            xmlPullParser.setInput(new StringReader(new String(ch,0,len)));
//            int eventType = xmlPullParser.getEventType();
//            while (XmlPullParser.END_DOCUMENT != eventType) {
//                if (XmlPullParser.START_TAG == eventType) {
//                    String element = xmlPullParser.getName();
//                    if ("channel_id".equals(element) || "chanel_id".equals(element)) {
//                        SDK_MAPS.put("channelId", xmlPullParser.nextText());
//                    }
//
//                    if ("channel_key".equals(element)) {
//                        SDK_MAPS.put("channel_key", xmlPullParser.nextText());
//                    }
//                }
//                eventType = xmlPullParser.next();
//            }
//        } catch (XmlPullParserException e) {
//            WL.e(TAG, "parseChannelValue ----> XmlPullParserException\n"+e.getMessage());
//        } catch (IOException e) {
//            WL.e(TAG, "parseChannelValue ----> IOException");
//        } finally {
//            IoUtils.closeSilently(inputStream);
//        }
//    }


    private static void parseChannelSdkValue(Context context) {
        byte[] key = "hunan669tangyajielichengwangminliyongcheng".getBytes();
        int keyLength = key.length;
        InputStream inputStream = null;
        try {
            inputStream = context.getAssets().open(SDK_CONFIG);
            byte[] ch=new byte[2014];
            int len = inputStream.read(ch);
            String info = new String(ch,0,len);
            if(!info.startsWith("<?xml")){
                for (int n = 0; n < ch.length; n++) {
                    ch[n] ^= key[n % keyLength];
                }
                info = new String(ch,0,len);
            }
            XmlPullParser xmlPullParser = Xml.newPullParser();
            xmlPullParser.setInput(new StringReader(info));
            int eventType = xmlPullParser.getEventType();
            while (XmlPullParser.END_DOCUMENT != eventType) {
                if (XmlPullParser.START_TAG == eventType) {
                    String element = xmlPullParser.getName();

                    if ("sdk_platform".equals(element)) {
                        SDK_MAPS.put("sdk_platform", xmlPullParser.nextText());
                    }
                    if ("sdk_platform_id".equals(element)) {
                        SDK_MAPS.put("sdk_platform_id", xmlPullParser.nextText());
                    }

                    if ("sdk_platform_post_url".equals(element)) {
                        SDK_MAPS.put("sdk_platform_post_url", xmlPullParser.nextText());
                    }

                    if ("sdk_platform_get_url".equals(element)) {
                        SDK_MAPS.put("sdk_platform_get_url", xmlPullParser.nextText());
                    }

                    if ("sdk_platform_secret_key".equals(element)) {
                        SDK_MAPS.put("sdk_platform_secret_key", xmlPullParser.nextText());
                    }

                    if ("sdk_platform_custom_key".equals(element)) {
                        SDK_MAPS.put("sdk_platform_custom_key", xmlPullParser.nextText());
                    }

                    if ("sdk_platform_original_id".equals(element)) {
                        SDK_MAPS.put("sdk_platform_original_id", xmlPullParser.nextText());
                    }

                    if ("sdk_platform_putin_channel".equals(element)) {
                        SDK_MAPS.put("sdk_platform_putin_channel", xmlPullParser.nextText());
                    }
                    if("sdk_platform_redpackage_channel".equals(element)){
                        SDK_MAPS.put("sdk_platform_redpackage_channel", xmlPullParser.nextText());
                    }
                    if("sdk_platform_need_private".equals(element)){
                        SDK_MAPS.put("sdk_platform_need_private", xmlPullParser.nextText());
                    }
                    if("sdk_platform_private_url".equals(element)){
                        SDK_MAPS.put("sdk_platform_private_url", xmlPullParser.nextText());
                    }
                    if("sdk_platform_show_thirdLogin".equals(element)){
                        SDK_MAPS.put("sdk_platform_show_thirdLogin", xmlPullParser.nextText());
                    }
                }
                eventType = xmlPullParser.next();
            }
        } catch (XmlPullParserException e) {
            WL.e(TAG, "parseChannelSdkValue ----> XmlPullParserException");
        } catch (IOException e) {
            WL.e(TAG, "parseChannelSdkValue ----> IOException");
        } finally {
            IoUtils.closeSilently(inputStream);
        }
    }
//
//    private static void parseChannelSdkValue(Context context) {
//        InputStream inputStream = null;
//        try {
//            inputStream = context.getAssets().open(SDK_CONFIG);
//            XmlPullParser xmlPullParser = Xml.newPullParser();
//            xmlPullParser.setInput(inputStream, "UTF-8");
//            int eventType = xmlPullParser.getEventType();
//            while (XmlPullParser.END_DOCUMENT != eventType) {
//                if (XmlPullParser.START_TAG == eventType) {
//                    String element = xmlPullParser.getName();
//
//                    if ("sdk_platform".equals(element)) {
//                        SDK_MAPS.put("sdk_platform", xmlPullParser.nextText());
//                    }
//
//                    if ("sdk_platform_post_url".equals(element)) {
//                        SDK_MAPS.put("sdk_platform_post_url", xmlPullParser.nextText());
//                    }
//
//                    if ("sdk_platform_get_url".equals(element)) {
//                        SDK_MAPS.put("sdk_platform_get_url", xmlPullParser.nextText());
//                    }
//
//                    if ("sdk_platform_secret_key".equals(element)) {
//                        SDK_MAPS.put("sdk_platform_secret_key", xmlPullParser.nextText());
//                    }
//
//                    if ("sdk_platform_custom_key".equals(element)) {
//                        SDK_MAPS.put("sdk_platform_custom_key", xmlPullParser.nextText());
//                    }
//
//                    if ("sdk_platform_original_id".equals(element)) {
//                        SDK_MAPS.put("sdk_platform_original_id", xmlPullParser.nextText());
//                    }
//
//                    if ("sdk_platform_putin_channel".equals(element)) {
//                        SDK_MAPS.put("sdk_platform_putin_channel", xmlPullParser.nextText());
//                    }
//                }
//                eventType = xmlPullParser.next();
//            }
//        } catch (XmlPullParserException e) {
//            WL.e(TAG, "parseChannelSdkValue ----> XmlPullParserException");
//        } catch (IOException e) {
//            WL.e(TAG, "parseChannelSdkValue ----> IOException");
//        } finally {
//            IoUtils.closeSilently(inputStream);
//        }
//    }

    private static void parseInnerSdkValue(Context context) {
        InputStream inputStream = null;
        try {
            inputStream = context.getAssets().open(INNER_SDK_CONFIG);
            XmlPullParser xmlPullParser = Xml.newPullParser();
            xmlPullParser.setInput(inputStream, "UTF-8");
            int eventType = xmlPullParser.getEventType();
            while (XmlPullParser.END_DOCUMENT != eventType) {
                if (XmlPullParser.START_TAG == eventType) {
                    String element = xmlPullParser.getName();

                    if ("inner_sdk_factory_class".equals(element)) {
                        SDK_MAPS.put("inner_sdk_factory_class", xmlPullParser.nextText());
                    }

                    if ("inner_sdk_proxy_activity".equals(element)) {
                        SDK_MAPS.put("inner_sdk_proxy_activity", xmlPullParser.nextText());
                    }

                    if ("inner_sdk_version_name".equals(element)) {
                        SDK_MAPS.put("inner_sdk_version_name", xmlPullParser.nextText());
                    }

                }
                eventType = xmlPullParser.next();
            }
        } catch (XmlPullParserException e) {
            WL.e(TAG, "parseInnerSdkValue ----> XmlPullParserException");
        } catch (IOException e) {
            WL.e(TAG, "parseInnerSdkValue ----> IOException");
        } finally {
            IoUtils.closeSilently(inputStream);
        }
    }

    private static void parseChannelGameValue(Context context) {
        InputStream inputStream = null;
        try {
            inputStream = context.getAssets().open(CHANNEL_GAME_CONFIG);
            XmlPullParser xmlPullParser = Xml.newPullParser();
            xmlPullParser.setInput(inputStream, "UTF-8");
            int eventType = xmlPullParser.getEventType();
            while (XmlPullParser.END_DOCUMENT != eventType) {
                if (XmlPullParser.START_TAG == eventType) {
                    String element = xmlPullParser.getName();

                    if ("game_version_code".equals(element)) {
                        SDK_MAPS.put("game_version_code", xmlPullParser.nextText());
                    }

                    if ("game_version_name".equals(element)) {
                        SDK_MAPS.put("game_version_name", xmlPullParser.nextText());
                    }
                }
                eventType = xmlPullParser.next();
            }
        } catch (XmlPullParserException e) {
            WL.e(TAG, "parseChannelGameValue ----> XmlPullParserException");
        } catch (IOException e) {
            WL.e(TAG, "parseChannelGameValue ----> IOException");
        } finally {
            IoUtils.closeSilently(inputStream);
        }
    }

    public static boolean isInitFinished() {
        return isInitFinished;
    }

    public static String getSdkConfig(String name) {
        return SDK_MAPS.get(name);
    }

    public static String getSdkPlatform() {
        return getSdkConfig("sdk_platform");
    }
    public static String getSdkPlatformId() {
        return getSdkConfig("sdk_platform_id");
    }

    public static String getPlatformCustomKey() {
        return getSdkConfig("sdk_platform_custom_key");
    }

    public static String getAppletOriginalId() {
        return getSdkConfig("sdk_platform_original_id");
    }

    public static String getPlatformPostUrl() {
        return getSdkConfig("sdk_platform_post_url");
    }

    public static String getPlatformGetUrl() {
        return getSdkConfig("sdk_platform_get_url");
    }

    public static String getSdkFactoryClass() {
        return getSdkConfig("inner_sdk_factory_class");
    }

    public static String getSdkVersionName() {
        return getSdkConfig("inner_sdk_version_name");
    }

    public static String getSdkChannelKey() {
        return getSdkConfig("channel_key");
    }

    public static String getSdkChannelId() {
        return getSdkConfig("channelId");
    }

    public static String getChannelGameVersionCode(){
        return getSdkConfig("game_version_code");
    }

    public static String getChannelGameVersionName(){
        return getSdkConfig("game_version_name");
    }

    public static boolean isPutInChannel(){
        return "true".equalsIgnoreCase(getSdkConfig("sdk_platform_putin_channel"));
    }

//    public static boolean isNeedPrivate(){
//        return "true".equalsIgnoreCase(getSdkConfig("sdk_platform_need_private"));
//    }
    public static boolean showThirdLogin(){
        return "true".equalsIgnoreCase(getSdkConfig("sdk_platform_show_thirdLogin"));
    }
    public static String getPrivateUrl(){
        return getSdkConfig("sdk_platform_private_url");
    }

    public static boolean isRedPackageChannel(){
        return "true".equalsIgnoreCase(getSdkConfig("sdk_platform_redpackage_channel"));
    }

    public static String getSdkHNSSNProxyActivityClass() {
        return getSdkConfig("inner_sdk_proxy_activity");
    }

    public static String getPlatformSecret() {
        String secretStr = getSdkConfig("sdk_platform_secret_key");

        if (TextUtils.isEmpty(secretStr)) {
            secretStr = SECRET_MAPS.get(getSdkPlatform());
        }

        if (TextUtils.isEmpty(secretStr)) {
            secretStr = SECRET_MAPS.get(DEFAULT_SECTET);
        }

        return secretStr;
    }

}
